<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="hadd"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="list">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{row}">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary" @click="doEdit(row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="dodele(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                :current-page="page"
                :page-sizes="[2, 5, 10, 20]"
                :page-size="pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 弹层 -->
      <el-dialog
        :title="isEdit?'编辑':'新增'"
        :visible.sync="dialogFormVisible"
      >
        <el-form ref="refform" :model="form" :rules="rules">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="form.name" autocomplete="off" />
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input v-model="form.description" autocomplete="off" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="hqu">取 消</el-button>
          <el-button type="primary" @click="hClik">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getEmployeeSimple, deletEmployeeSimple, AddEmployeeSimple, EditoyeeSimple } from '@/api/setting'
export default {
  data() {
    return {
    // 分页内容
      page: 1,
      pagesize: 2,
      total: 0,
      list: [],
      //   弹层
      dialogFormVisible: false,
      form: {
        name: '',
        description: ''
      },
      isEdit: false,
      //   表单验证规则
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.loadmployeeSimple()
  },
  methods: {
    // 分页函数
    handleSizeChange(val) {
      this.pagesize = val
      this.loadmployeeSimple()
    },
    handleCurrentChange(val) {
      this.page = val
      this.loadmployeeSimple()
    },
    // 全部角色
    async loadmployeeSimple() {
      try {
        const res = await getEmployeeSimple(this.page, this.pagesize)
        this.list = res.data.rows
        this.total = res.data.total
        console.log(res, '角色信息')
      } catch (error) {
        console.log(error)
      }
    },
    // 删除函数
    async doeletEmployeeSimple(id) {
      try {
        await deletEmployeeSimple(id)
        if (this.list.length === 1) {
          this.page--
          if (this.page <= 0) {
            this.page = 1
          }
        }
        this.loadmployeeSimple()
      } catch (error) {
        console.log(error)
      }
    },
    // 确认删除
    dodele(id) {
      this.$confirm('是否删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.doeletEmployeeSimple(id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },

    // 新增
    async AddloadloyeeSimple() {
      try {
        await AddEmployeeSimple(this.form)
        this.loadmployeeSimple()
        this.form = {}
      } catch (error) {
        console.log(error)
      }
    },
    // 新增按钮
    hadd() {
      this.isEdit = false
      this.dialogFormVisible = true
      this.form = {}
      this.tota++
      const a = Math.ceil(this.total / this.pagesize)
      this.page = a
    },
    // 编辑
    async loadEditoyeeSimple(id) {
      try {
        await EditoyeeSimple(id)
      } catch (error) {
        console.log(error)
      }
    },
    // 编辑按钮
    doEdit(row) {
      this.isEdit = true
      this.dialogFormVisible = true
      this.form = row
    },
    //   确定按钮
    hClik() {
    // 兜底校验
      this.$refs.refform.validate(valid => {
        if (!valid) return
        this.isEdit ? this.loadEditoyeeSimple() : this.AddloadloyeeSimple()
        this.dialogFormVisible = false
        this.form = {}
      })
    },
    hqu() {
      this.dialogFormVisible = false
      this.form = {}
    }

  }

}
</script>
